home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATETIME.SWG / 0021_OOP Calendar Unit.pas < prev    next >
Pascal/Delphi Source File  |  1993-06-22  |  4KB  |  170 lines

  1. UNIT CalUnit;
  2. { Object oriented calander unit }
  3.  
  4. INTERFACE
  5.  
  6. USES CRT,DOS;
  7.  
  8. TYPE
  9.   Calendar = OBJECT
  10.     ThisMonth, ThisYear : Word;
  11.     CONSTRUCTOR Init(Month, Year: Integer);
  12.     PROCEDURE        DrawCalendar;
  13.     PROCEDURE        SetMonth(Month: Integer);
  14.     PROCEDURE        SetYear(Year: Integer);
  15.     FUNCTION        GetMonth: Integer;
  16.     FUNCTION        GetYear: Integer;
  17.     DESTRUCTOR        Done;
  18.   END;
  19.  
  20. IMPLEMENTATION
  21.  
  22. CONSTRUCTOR Calendar.Init(Month, Year: Integer);
  23. BEGIN
  24.    SetYear(Year);
  25.    SetMonth(Month);
  26.    DrawCalendar;
  27. END;
  28.  
  29. PROCEDURE Calendar.DrawCalendar;
  30.  
  31. VAR
  32.   CurYear,CurMonth,CurDay,CurDow,
  33.   ThisDay,ThisDOW    : Word;
  34.   I,DayPos,NbrDays   : Byte;
  35.  
  36. CONST
  37.   DOM: ARRAY[1..12] OF Byte =
  38.        (31,28,31,30,31,30,31,31,30,31,30,31);
  39.   MonthName: ARRAY[1..12] OF String[3] =
  40.        ('Jan','Feb','Mar','Apr','May','Jun',
  41.         'Jul','Aug','Sep','Oct','Nov','Dec');
  42.  
  43. BEGIN
  44.  
  45.   GetDate(CurYear,CurMonth,CurDay,CurDow);
  46.  
  47.   {Set to day 1 so we can use GetDate function}
  48.   ThisDay := 1;
  49.  
  50.   SetDate(ThisYear,ThisMonth,ThisDay);
  51.  
  52.   {ThisDOW stands for This day of the week}
  53.  
  54.   GetDate(ThisYear,ThisMonth,ThisDay,ThisDOW);
  55.  
  56.   SetDate(CurYear,CurMonth,CurDay);
  57.  
  58.   WriteLn('           ',MonthName[ThisMonth],
  59.           ' ',ThisYear);
  60.   WriteLn;
  61.   WriteLn('   S   M   T   W   R   F   S');
  62.  
  63.   NbrDays := DOM[ThisMonth];
  64.  
  65.   {Check for leap year, which occurs when the
  66.    year is evenly divisible by 4 and not evenly
  67.    divisable by 100 or if the year is evenly
  68.    divisable by 400}
  69.  
  70.   IF ((ThisMonth = 2) AND
  71.      ((ThisYear MOD 4 = 0) AND
  72.       (ThisYear MOD 100 <> 0))
  73.      OR (ThisYear MOD 400 = 0))
  74.    THEN NbrDays := 29;
  75.  
  76.   FOR I:= 1 TO NbrDays DO
  77.     BEGIN
  78.       DayPos := ThisDOW * 4 + 2;  {Position day #}
  79.       GotoXY(DayPos,WhereY);
  80.       Inc(ThisDOW);
  81.       Write(I:3);
  82.       IF ThisDOW > 6 THEN
  83.         BEGIN
  84.           ThisDOW := 0;
  85.           WriteLn
  86.         END
  87.     END;
  88.     WriteLn
  89. END;
  90.  
  91. PROCEDURE Calendar.SetMonth(Month: Integer);
  92. BEGIN
  93.    ThisMonth := Month;
  94.    WHILE ThisMonth < 1 DO
  95.    BEGIN
  96.       Dec(ThisYear);
  97.       Inc(ThisMonth, 12);
  98.    END;
  99.    WHILE ThisMonth > 12 DO
  100.    BEGIN
  101.       Inc(ThisYear);
  102.       Dec(ThisMonth, 12);
  103.    END;
  104. END;
  105.  
  106. PROCEDURE Calendar.SetYear(Year: Integer);
  107. BEGIN
  108.    ThisYear := Year;
  109. END;
  110.  
  111. FUNCTION Calendar.GetMonth: Integer;
  112. BEGIN
  113.    GetMonth := ThisMonth;
  114. END;
  115.  
  116. FUNCTION Calendar.GetYear: Integer;
  117. BEGIN
  118.    GetYear := ThisYear;
  119. END;
  120.  
  121. DESTRUCTOR Calendar.Done;
  122. BEGIN
  123.    {for dynamic object instances,
  124.      the Done method still works even
  125.      though it contains nothing except
  126.      the destructor declaration              }
  127. END;
  128.  
  129. END.
  130.  
  131. { ---------------------------    TEST PROGRAM ---------------------}
  132. PROGRAM CalTest;
  133.  
  134. USES DOS,CRT,CalUnit;
  135.  
  136. VAR
  137.    MyCalendar: Calendar;
  138.    TYear,TMonth,Tday,TDOW: Word;
  139.  
  140. BEGIN
  141.    ClrScr;
  142.    GetDate(TYear,TMonth,Tday,TDOW);
  143.    WITH MyCalendar DO
  144.    BEGIN
  145.       WriteLn('    Current Month''s Calendar');
  146.       WriteLn;
  147.       Init(TMonth, TYear);
  148.       WHILE (TMonth <> 0) DO
  149.         BEGIN
  150.           WriteLn;
  151.           WriteLn('   Enter a Month and Year');
  152.           WriteLn('(Separate values by a space)');
  153.           WriteLn;
  154.           WriteLn('      exm.      3 1990');
  155.           WriteLn;
  156.           Write         ('   or 0 0 to quit: ');
  157.           ReadLn(TMonth, TYear);
  158.           IF TMonth <> 0 THEN
  159.              BEGIN
  160.                ClrScr;
  161.                SetYear(TYear);
  162.                SetMonth(TMonth);
  163.                DrawCalendar
  164.              END
  165.         END
  166.    END;
  167.    ClrScr
  168. END.
  169.  
  170.